package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.StoreContractGift;
import com.dingreading.cloud.admin.entity.table.StoreContractGiftTableDef;
import com.dingreading.cloud.admin.mapper.StoreContractGiftMapper;
import com.dingreading.cloud.admin.service.StoreContractGiftService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-12-04
 */
@Service
public class StoreContractGiftServiceImpl extends BaseServiceImpl<StoreContractGiftMapper, StoreContractGift, StoreContractGiftTableDef> implements StoreContractGiftService {

    @Override
    protected StoreContractGiftTableDef getTable() {
        return StoreContractGiftTableDef.storeContractGift;
    }

    @Override
    public void deleteByIdsNotIn(Long contractId, List<Long> ids) {
        QueryCondition condition = table.contractId.eq(contractId)
                .and(table.id.notIn(ids))
                .and(table.status.eq(0));
        getMapper().deleteByCondition(condition);
    }

    @Override
    public List<StoreContractGift> listByContractIdIn(List<Long> contractIds) {
        return getMapper().selectListByCondition(table.contractId.in(contractIds));
    }

    @Override
    public List<StoreContractGift> listByContractId(Long id) {
        return getMapper().selectListByCondition(table.contractId.eq(id));
    }

    @Override
    public boolean setGiftDone(Long giftId, Integer status, String partRemark, String remark) {
        UpdateChain<StoreContractGift> updateChain = UpdateChain.of(StoreContractGift.class);
        if (StringUtils.isNotBlank(partRemark))
            updateChain.set(table.partRemark, partRemark);
        return updateChain.set(table.remark, remark)
                .set(table.status, status)
                .where(table.id.eq(giftId))
                .update();
    }

    @Override
    public void deleteByContract(Long contractId) {
        QueryCondition condition = table.contractId.eq(contractId)
                .and(table.status.eq(0));
        getMapper().deleteByCondition(condition);
    }
}
